<template>
  <div>
    <el-dialog title="笔试邀请" :visible.sync="dialogVisible" width="60%">
      <el-row style="margin-top:1rem;">
        <el-col :span="3">
          <div style="text-align:right;padding:0.5rem;">模板名：</div>
        </el-col>
        <el-col :span="20">
          <el-input placeholder="模板名不可重复" v-model="q.name" clearable>
          </el-input>
        </el-col>
      </el-row>
      <el-row style="margin-top:1rem;">
        <el-col :span="3">
          <div style="text-align:right;padding:0.5rem;">收件人：</div>
        </el-col>
        <el-col :span="20">
          <el-input placeholder="请输入内容" v-model="q.sendTo" clearable>
          </el-input>
        </el-col>
      </el-row>
      <el-row style="margin-top:1rem;">
        <el-col :span="3">
          <div style="text-align:right;padding:0.5rem;">抄送：</div>
        </el-col>
        <el-col :span="20">
          <el-input placeholder="请输入内容" v-model="q.cc" clearable>
          </el-input>
        </el-col>
      </el-row>
      <el-row style="margin-top:1rem;">
        <el-col :span="3">
          <div style="text-align:right;padding:0.5rem;">主题格式：</div>
        </el-col>
        <el-col :span="20">
          <el-input placeholder="请输入内容" v-model="q.subjectPrefix" clearable>
          </el-input>
        </el-col>
      </el-row>
      <el-row style="margin-top:1rem;">
        <el-col :span="3">
          <div style="text-align:right;padding:0.5rem;">附件：</div>
        </el-col>
        <el-col :span="21">
             <el-checkbox-group v-model="attachmentChecklist" style="margin-top:0.5rem">
            <el-checkbox v-for="item of options" :key="item.id" :label="item.id">{{item.name}}</el-checkbox>
        </el-checkbox-group>
        </el-col>
      </el-row>
      <el-row style="margin-top:1rem;">
        <el-col :span="3">
          <div style="text-align:right;padding:0.5rem;">正文：</div>
        </el-col>
        <el-col :span="20">
          <el-input type="textarea" placeholder="请输入内容" v-model="q.content" rows="15">
          </el-input>
        </el-col>
      </el-row>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import _ from 'lodash'
  export default {
    data: function () {
      return {
        dialogVisible: false,
        q: {},
        options: [],
        attachmentChecklist:[]
      }
    },
    methods: {
      init() {
        this.dialogVisible = true
        this.getAttachment()
        this.getWrittenExaminationInvitation()
      },
      getAttachment(){
        this.$http({
          url: `${this.$http.adornUrl('/attachment/list?token='+this.$cookie.get('token') )}`,
          method: 'get',
        }).then(data => {
          if(data.data.code==0){
              console.log(data)
              this.options = data.data.list
          }else{
            this.$message({
                message: '获取附件列表失败',
                type: 'error'
              });
          }
        })
      },
      getWrittenExaminationInvitation(){
          this.$http({
          url: `${this.$http.adornUrl('/emailDict/writtenExaminationInvitation?token='+this.$cookie.get('token') )}`,
          method: 'get',
        }).then(data => {
          if(data.data.code==0){
              console.log(data)
              this.q = data.data.writtenExaminationInvitation
              this.attachmentChecklist = JSON.parse(this.q.attachment)
          }else{
            this.$message({
                message: '获取笔试邀请失败',
                type: 'error'
              });
          }
        })
      },
      submit() {
        let standard_attachments = new Set(_.map(this.options,"id"))
        let attachments = this.attachmentChecklist.filter(item =>standard_attachments.has(item))
        console.log(attachments)
        this.q.attachment = JSON.stringify(attachments)
        this.$http({
          url: `${this.$http.adornUrl('/emailDict/?token='+this.$cookie.get('token') )}`,
          method: 'put',
          data: this.$http.adornParams(this.q)
        }).then(data => {
          if(data.data.code==0){
            this.$message({
                message: '更新成功',
                type: 'success'
              });
              this.$emit('fnc')
              this.dialogVisible = false
          }else{
            this.$message({
                message: '更新失败',
                type: 'error'
              });
          }
        })
      }
    }

  }

</script>
